//
//  VisitorView.swift
//  WeiBo
//
//  Created by 陈贵 on 15/9/7.
//  Copyright © 2015年 hzj. All rights reserved.
//




import UIKit

//protocol VisitorViewDegelate:NSObjectProtocol{
//    
//    func clickTheZhuCeBth()
//    func clickTheLoginBth()
//}


class VisitorView: UIView {
    
//    weak var delegate:VisitorViewDegelate?
//    @objc func clikTheZhuCeBth(){
//        delegate?.clickTheZhuCeBth()
//    }
//    @objc func clikTheDengLuBth(){
//        delegate?.clickTheLoginBth()
//    }
    
    //初始化界面
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //MARK: - 设置视图信息
    func setUIMessage(isHome:Bool ,imageName:NSString,text:String){
        
        iconView.image = UIImage(named: imageName as String)
        textLable.text = text
        homeView.hidden = !isHome
        
        //MARK:-待修改
        isHome ? startAnimation() : sendSubviewToBack(theMaskView)
        
    }
    
    //主页的转动动画
    private func startAnimation(){
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2 * M_PI
        animation.duration = 10
        animation.repeatCount = MAXFLOAT
        animation.removedOnCompletion = false
        iconView.layer.addAnimation(animation, forKey: nil)
        
        
    }
    

    
    //MARK: - 设置界面
    private func setUI(){
        //添加子控件
        addSubview(iconView)
        addSubview(theMaskView)
        addSubview(homeView)
        addSubview(textLable)
        addSubview(registerBth)
        addSubview(loginBth)
        
//        添加约束
        //图片
        iconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -20))
//        遮挡图片
        theMaskView.translatesAutoresizingMaskIntoConstraints = false
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[v]-0-|", options: [], metrics: nil, views: ["v":theMaskView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[v]-200-|", options: [], metrics: nil, views: ["v":theMaskView]))


        //房子
        homeView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))

        //文字
        textLable.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: textLable, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: textLable, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 20))
        addConstraint(NSLayoutConstraint(item: textLable, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 240))
        //"注册"按钮
        registerBth.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registerBth, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: textLable, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBth, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: textLable, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 20))
        addConstraint(NSLayoutConstraint(item: registerBth, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerBth, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 40))


        //"登陆"按钮
        loginBth.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginBth, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: textLable, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBth, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: textLable, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 20))
        addConstraint(NSLayoutConstraint(item: loginBth, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBth, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 40))

        //设置背景色
        backgroundColor = UIColor(red: 237/255.0, green: 237/255.0, blue: 237/255.0, alpha: 1)
        
        
    }
    
//MARK: - 懒加载子控件
    //图片
    private lazy var iconView:UIImageView = {
        let iconView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        return iconView
        }()
//    遮挡图片
    private lazy var theMaskView:UIImageView = {
        let theMaskView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        return theMaskView
    }()
    //房子图标
    private lazy var homeView:UIImageView = {
        let homeView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return homeView
    }()
    //文字
    private lazy var textLable:UILabel = {
        let textLable = UILabel()
        textLable.textAlignment = NSTextAlignment.Center
        textLable.font = UIFont.systemFontOfSize(13)
        textLable.numberOfLines = 0
        textLable.text = "清风拂面，再回首，难忆往昔；聚聚离离，欢欢乐乐，皆默然"
        textLable.textColor = UIColor(white: 0.53, alpha: 1)
        return textLable
    }()
    
    
    //将两按钮设置为共有属性，以便于可在BasicViewController中添加相应事件，而不使用通知·监听·代理等方法
    //“注册”按钮
     lazy var registerBth:UIButton = {
        let bth = UIButton()
        bth.setTitle("注册", forState: UIControlState.Normal)
        bth.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        bth.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
//        bth.addTarget(self, action: "clikTheZhuCeBth", forControlEvents: UIControlEvents.TouchUpInside)
        
        return bth
    }()
    //“登陆”按钮
     lazy var loginBth:UIButton = {
        let bth = UIButton()
        bth.setTitle("登陆", forState: UIControlState.Normal)
        bth.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        bth.setTitleColor(UIColor.brownColor(), forState: UIControlState.Normal)
//        bth.addTarget(self, action: "clikTheDengLuBth", forControlEvents: UIControlEvents.TouchUpInside)
        return bth
        }()

    

}
